﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SZWCFBatteryService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“Service1”。
    public class BatteryService : IBatteryService
    {
        public string GetData(int value)
        {
            return string.Format("你好，我是时代高科数据服务器，你发的信息: {0}", value);
        }
        
        public string TrayBatService(string trayBattery)
        {
            BatteryResponse myResponse = new BatteryResponse();
            string stoveNum = "";
            string istoveNum = "";
            try
            {
                ConfigOperate.PrintLog(trayBattery);
                //解析json数据为实体类
                TrayBattery myTrayBattery = ConfigTool.DataContractJsonDeserialize<TrayBattery>(trayBattery.Replace("\0", ""));
                //创建数据库连接
                DataBaseOperate mySqlUtil = new DataBaseOperate();
                List<String> batteryList = myTrayBattery.BatteryList;
                //根据行列号获取炉号
                DataTable stoveTable = mySqlUtil.loadDate(string.Format("select t1.stoveNum from StoveLineColumn t1 where t1.stoveLine = {0} and t1.stoveColumn = {1}", Convert.ToInt32(myTrayBattery.LineNumber), Convert.ToInt32(myTrayBattery.ColumnNumber)));
                
                //判断是否存在炉号
                if (stoveTable.Rows.Count > 0)
                {
                    stoveNum = stoveTable.Rows[0][0].ToString();
                }
                else
                {
                    //未查询到炉号               
                    myResponse.SignSuccess = false;
                    myResponse.SignMessage = "未获取到炉号信息，请检查行列层数据。";
                    string strResponseCat = ConfigTool.DataContractJsonSerialize<BatteryResponse>(myResponse);
                    return strResponseCat.Replace("\0", "");
                }
                /***********************************生成系统内部炉号***********************************/
                DateTime nowTime = DateTime.Now;
                DataTable istoveNumTable = mySqlUtil.loadDate(string.Format("select t1.istoveNum,t1.currentTime from StoveNumRule t1 where t1.currentTime > '{0}' and t1.istoveNum like '%{1}%' order by t1.currentTime desc", nowTime.AddDays(-1), stoveNum));
                
                if (istoveNumTable.Rows.Count > 0)
                {
                    DateTime firstTime = (DateTime)istoveNumTable.Rows[0][1];
                    if (DateTime.Compare(firstTime, nowTime.AddHours(-2)) > 0)
                    {
                        istoveNum = istoveNumTable.Rows[0][0].ToString();
                    }
                    else
                    {
                        string tempNum = istoveNumTable.Rows[0][0].ToString();
                        string yesterdayDay = tempNum.Substring(0, 8);
                        if (yesterdayDay == DateTime.Now.ToString("yyyyMMdd"))
                        {
                            string serialNumber = tempNum.Substring(13, 3);
                            int serialNumAdd = Convert.ToInt32(serialNumber) + 1;
                            istoveNum = tempNum.Substring(0, 13) + serialNumAdd;
                            mySqlUtil.loadDate(string.Format("insert into StoveNumRule (istoveNum) values('{0}') ", istoveNum));
                        }
                        else
                        {
                            istoveNum = DateTime.Now.ToString("yyyyMMdd") + "-" + stoveNum + "-" + "101";
                            mySqlUtil.loadDate(string.Format("insert into StoveNumRule (istoveNum) values('{0}') ", istoveNum));
                        }
                    }
                    
                }
                else
                {
                    istoveNum = DateTime.Now.ToString("yyyyMMdd") + "-" + stoveNum + "-" + "101";
                    mySqlUtil.loadDate(string.Format("insert into StoveNumRule (istoveNum) values('{0}') ", istoveNum));
                }

                //遍历电池码添加到数据库
                for (int i = 0; i < batteryList.Count; i++)
                {
                    //往数据库中插入数据
                    mySqlUtil.loadDate(string.Format("insert into TrayBatteryRelation (trayNo,batteryNo,stoveLine,stoveColumn,stoveLayer,toStorageTime,spare1,spare2,spare3,spare4,spare5,stoveNum,istoveNum,SZtrayNo)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')",
                        myTrayBattery.TrayNo, batteryList[i], myTrayBattery.LineNumber, myTrayBattery.ColumnNumber, myTrayBattery.LayerNumber, myTrayBattery.WarehousingTime,
                        myTrayBattery.Spare1, myTrayBattery.Spare2, myTrayBattery.Spare3, myTrayBattery.Spare4, myTrayBattery.Spare5, stoveNum, istoveNum,
                        DateTime.Now.ToString("yyyyMMdd") + myTrayBattery.TrayNo));
                }
            }
            catch (Exception ex)
            {
                //解析数据失败返回失败详情                
                myResponse.SignSuccess = false;
                myResponse.SignMessage = "数据解析错误,请检查传入参数。错误详情：" + ex.Message + "----" + stoveNum + istoveNum;
                ConfigOperate.PrintLog(ex.Message);
                string strResponseCat = ConfigTool.DataContractJsonSerialize<BatteryResponse>(myResponse);
                return strResponseCat.Replace("\0", "");
            }

            //返回成功
            myResponse.SignSuccess = true;
            myResponse.SignMessage = "成功";
            string strResponse = ConfigTool.DataContractJsonSerialize<BatteryResponse>(myResponse);
            return strResponse.Replace("\0","");
        }
    }
}
